package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.model.MarketCouponUser;
import com.cskaoyan.model.MarketCouponUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/13
 */
@Service
public class CouponUserServiceImpl implements CouponUserService {
    @Autowired
    MarketCouponUserMapper couponUserMapper;


    @Override
    public Integer getAvailableCouponLength(Integer userId, Integer couponId) {
        Date current = new Date();
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.createCriteria()
                .andUserIdEqualTo(userId)
                .andCouponIdEqualTo(couponId)
                .andStartTimeLessThanOrEqualTo(current)
                .andEndTimeGreaterThanOrEqualTo(current)
                .andDeletedEqualTo(false);
        List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(example);

        return marketCouponUsers.size();
    }

    @Override
    public List<MarketCouponUser> getCouponList(Integer userId) {
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.createCriteria()
                .andUserIdEqualTo(userId)
                .andDeletedEqualTo(false);
        List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(example);
        return marketCouponUsers;
    }

    @Override
    public MarketCouponUser findById(Integer userCouponId) {
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.createCriteria()
                .andIdEqualTo(userCouponId)
                .andDeletedEqualTo(false);
        List<MarketCouponUser> marketCouponUsers = couponUserMapper.selectByExample(example);
        return marketCouponUsers!=null&&marketCouponUsers.size()==1?marketCouponUsers.get(0):null;
    }

    @Override
    public boolean delById(Integer userCouponId) {
        MarketCouponUser couponUser = new MarketCouponUser();
        couponUser.setDeleted(true);
        couponUser.setUpdateTime(new Date());
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.createCriteria()
                .andIdEqualTo(userCouponId)
                .andDeletedEqualTo(false);
        int affectRows = couponUserMapper.updateByExampleSelective(couponUser, example);
        return affectRows != 0;
    }


}
